package com.feib.soeasy.action;

import java.util.ArrayList;
import java.util.Iterator;

import com.feib.soeasy.model.CommissionCodeTable;
import com.feib.soeasy.pagination.PaginatedList;
import com.feib.soeasy.service.CommissionCodeTableService;

import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.DontBind;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.integration.spring.SpringBean;
import net.sourceforge.stripes.validation.LocalizableError;
import net.sourceforge.stripes.validation.ValidationErrorHandler;
import net.sourceforge.stripes.validation.ValidationErrors;

public class Soez9005ActionBean extends BaseActionBean implements ValidationErrorHandler {

    private static final String DEFAULT_JSP = "/WEB-INF/views/soez9005/soez9005.jsp";
    private static final String QUERY_LIST_JSP = "/WEB-INF/views/soez9005/soez9005List.jsp";

    private PaginatedList<CommissionCodeTable> queryCommissionCodeResult;

    private ArrayList<CommissionCodeTable> commissionCodeTables ;

    private String actionType;

    private String qryCode;

    private String qryCodeName;

    private String modifyMessage;

    @SpringBean("commissionCodeTableService")
    private CommissionCodeTableService commissionCodeTableService;

    @DontBind
    @DefaultHandler
    public Resolution doDefault()
    {
        logger.debug("Enter doDefault mehtod.");
        return new ForwardResolution(DEFAULT_JSP); 
    }


    public Resolution doAjaxQueryAction()
    {
        logger.debug("Enter doAjaxQueryAction mehtod.");
        try {

            queryCommissionCodeResult = 
                commissionCodeTableService.queryCommissionCodeTable(qryCode, qryCodeName);


        }catch (Exception e) {
            logger.error("系統程式查詢錯誤(Soez9005ActionBean.doAjaxQueryAction):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxQueryAction.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }
        return new ForwardResolution(QUERY_LIST_JSP);
    }

    public Resolution doAjaxModCommissionAction()
    {
        try {
            if("S".equals(actionType))
            {
                modifyCommissionCode();
            }else if("A".equals(actionType))
            {
                addCommissionCode();
            }			
        } catch (Exception e) {
            logger.error("系統程式維護錯誤(Soez9005ActionBean.doAjaxModCommissionAction):", e);

            // 轉換錯誤訊息
            ValidationErrors errors = new ValidationErrors();
            errors.addGlobalError(new LocalizableError("doAjaxModCommissionAction.exception") );
            getContext().setValidationErrors(errors);
            // 回傳 錯誤 HTML fragments
            return super.errorsToHtmlStreams(errors);
        }

        return new ForwardResolution(QUERY_LIST_JSP);
    }

    public void modifyCommissionCode()
    {
        queryCommissionCodeResult = 
            new PaginatedList<CommissionCodeTable>();

        for(Iterator<CommissionCodeTable> i=commissionCodeTables.iterator();i.hasNext();)
        {
            CommissionCodeTable commissionCodeTable = (CommissionCodeTable)i.next();
            if(null == commissionCodeTable.getCommissionCode() 
                            || "".equals(commissionCodeTable.getCommissionCode()))
                modifyMessage = "所有欄位都必須輸入!";
            if(null == commissionCodeTable.getCommissionCodeName() 
                            || "".equals(commissionCodeTable.getCommissionCodeName()))
                modifyMessage = "所有欄位都必須輸入!";
            if(null == commissionCodeTable.getAmountScopeS())
                modifyMessage = "所有欄位都必須輸入!";
            if(null == commissionCodeTable.getAmountScopeE())
                modifyMessage = "所有欄位都必須輸入!";
            if(null == commissionCodeTable.getCommissionType() 
                            || "".equals(commissionCodeTable.getCommissionType()))
                modifyMessage = "所有欄位都必須輸入!";
            if(null == commissionCodeTable.getCommissionAmount())
                modifyMessage = "所有欄位都必須輸入!";
            if(null == commissionCodeTable.getBankAmount())
                modifyMessage = "所有欄位都必須輸入!";
            if(null == commissionCodeTable.getGroupAmount())
                modifyMessage = "所有欄位都必須輸入!";
        }

        // 檢查代碼重覆
    	if(modifyMessage == null || "".equals(modifyMessage))
    	{
	    	for(int i=0;i<commissionCodeTables.size();i++)
	    	{
	    		CommissionCodeTable first = (CommissionCodeTable)commissionCodeTables.get(i);
	    		for(int j=0;j<commissionCodeTables.size();j++)
	        	{
	    			CommissionCodeTable next = (CommissionCodeTable)commissionCodeTables.get(j);
	    			if((i != j) &&
	    					first.getCommissionCode().equals(next.getCommissionCode()))
	    			{
	    				modifyMessage = "代碼不得重覆!!";
	    				break;
	    			}
	        	}
	    	}
    	}
         
        if(null == modifyMessage || "".equals(modifyMessage))
        {
            queryCommissionCodeResult.setPaginatedList(
                            commissionCodeTableService.modifyCommissionCodeTable(commissionCodeTables));

            modifyMessage = "更新成功!!";
        }else
        {
            queryCommissionCodeResult.setPaginatedList(commissionCodeTables);
        }
    }

    public void addCommissionCode()
    {
        queryCommissionCodeResult = 
            new PaginatedList<CommissionCodeTable>();

        if(null == commissionCodeTables)
            commissionCodeTables = new ArrayList<CommissionCodeTable>();

        commissionCodeTables.add(new CommissionCodeTable());
        queryCommissionCodeResult.setPaginatedList(commissionCodeTables);
    }

    public PaginatedList<CommissionCodeTable> getQueryCommissionCodeResult() {
        return queryCommissionCodeResult;
    }


    public void setQueryCommissionCodeResult(
                    PaginatedList<CommissionCodeTable> queryCommissionCodeResult) {
        this.queryCommissionCodeResult = queryCommissionCodeResult;
    }


    public ArrayList<CommissionCodeTable> getCommissionCodeTables() {
        return commissionCodeTables;
    }


    public void setCommissionCodeTables(
                    ArrayList<CommissionCodeTable> commissionCodeTables) {
        this.commissionCodeTables = commissionCodeTables;
    }


    public String getActionType() {
        return actionType;
    }


    public void setActionType(String actionType) {
        this.actionType = actionType;
    }


    public String getQryCode() {
        return qryCode;
    }


    public void setQryCode(String qryCode) {
        this.qryCode = qryCode;
    }


    public String getQryCodeName() {
        return qryCodeName;
    }


    public void setQryCodeName(String qryCodeName) {
        this.qryCodeName = qryCodeName;
    }


    public String getModifyMessage() {
        return modifyMessage;
    }


    public void setModifyMessage(String modifyMessage) {
        this.modifyMessage = modifyMessage;
    }


}